package com.zrwl.poscloud.xo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zrwl.poscloud.base.entity.Role;
import com.zrwl.poscloud.base.entity.UserRoleR;
import com.zrwl.poscloud.base.superI.SuperServiceImpl;
import com.zrwl.poscloud.commons.utils.ResultUtil;
import com.zrwl.poscloud.xo.global.MessageConf;
import com.zrwl.poscloud.xo.mapper.UserRoleRMapper;
import com.zrwl.poscloud.xo.service.RoleService;
import com.zrwl.poscloud.xo.service.UserRoleRService;
import com.zrwl.poscloud.xo.vo.UserRoleRVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 用户权限 服务实现类
 *
 * @author 刘思童 AutoGenerate
 * @date 2021-07-09
 */
@Service
public class UserRoleRServiceImpl extends SuperServiceImpl<UserRoleRMapper, UserRoleR> implements UserRoleRService {

    @Autowired
    private UserRoleRService userRoleRService;
    @Autowired
    private RoleService roleService;


    @Override
    public String add(UserRoleRVO userRoleRVO) {
        QueryWrapper<UserRoleR> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_uid", userRoleRVO.getUserUid());
        UserRoleR userRoleR = userRoleRService.getOne(queryWrapper);
        if (userRoleR == null) {
            QueryWrapper<Role>queryWrapperR=new QueryWrapper<>();
            queryWrapperR.eq("owner_type",5);
            queryWrapper.eq("is_super",4);
            queryWrapper.isNull("owner_uid");
            queryWrapper.eq("status",1);
            Role role=roleService.getOne(queryWrapperR);
            if(role!=null) {
                UserRoleR entity = new UserRoleR();
                entity.setUserUid(userRoleRVO.getUserUid());
                entity.setRoleUid(role.getUid());
                entity.insert();
                userRoleRVO.setUid(entity.getUid());
                return ResultUtil.successWithMessage(MessageConf.INSERT_SUCCESS);
            }
            else {
                return ResultUtil.errorWithMessage("错误无法授权");
            }
        }
        else {
            return ResultUtil.errorWithMessage("重复添加权限");
        }
    }
}
